package wli.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import wli.hibernate.Roles;
import wli.hibernate.Users;
import wli.hibernate.util.HibernateUtil;
import wli.java.security.util.Md5Encryption;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		Session hsession = null;
		Transaction transaction = null;
		
		String admin_option = request.getParameter("admin_option");
		if (admin_option == null) {
			out.println("<br><a href=admin?admin_option=add_roles>add_roles</a>");
			out.println("<br><a href=admin?admin_option=add_users>add_users</a>");
		} else if (admin_option.equals("add_roles")) {

			Roles usrl = new Roles();
			hsession = HibernateUtil.getSessionFactory().openSession();

			try {
				System.out.println("Test add user roles only....");
				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();

				// usrl.setRoleId(1);
				usrl.setRoleName("admin");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();
				hsession.close();

				System.out.println("Test add user roles only....");

				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(2);
				usrl.setRoleName("manager");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();
				hsession.close();

				System.out.println("Test add user roles only....");
				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(3);
				usrl.setRoleName("manager-gui");
				// usrl.setRoleOwner("wli");

				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();
				hsession.close();

				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(4);
				usrl.setRoleName("manager-script");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();
				hsession.close();

				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(5);
				usrl.setRoleName("manager-jmx");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();

				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(6);
				usrl.setRoleName("manager-status");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();

				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(7);
				usrl.setRoleName("members");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();

				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();
				// usrl.setRoleId(8);
				usrl.setRoleName("tomcat");
				// usrl.setRoleOwner("wli");
				System.out.println("User Role [" + hsession.save(usrl)
						+ "] Saved.");
				transaction.commit();
			} catch (HibernateException e) {
				transaction.rollback();
				System.out.println(">>>>>>>>>>>>>>>>>>>>" + e);
			} finally {
				hsession.close();
			}
		} else if (admin_option.equals("add_users")) {
			try {
				Set<Roles> role_set = new HashSet<Roles>();
				Roles usrl1 = new Roles();

				usrl1.setRoleName("admin");
				role_set.add(usrl1);

				Roles usrl2 = new Roles();

				usrl2.setRoleName("manager");
				role_set.add(usrl2);

				Roles usrl3 = new Roles();

				usrl3.setRoleName("manager-gui");
				role_set.add(usrl3);

				Roles usrl4 = new Roles();

				usrl4.setRoleName("manager-jmx");
				role_set.add(usrl4);
				// user.setUserRoles(role_set);

				Roles usrl5 = new Roles();

				usrl5.setRoleName("manager-status");
				role_set.add(usrl5);

				Roles usrl6 = new Roles();

				usrl6.setRoleName("members");
				role_set.add(usrl6);

				Roles usrl7 = new Roles();

				usrl7.setRoleName("tomcat");
				role_set.add(usrl7);

				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
						+ role_set.size());
				Users user = new Users();
				user.setUserName("wli");
				user.setPassWord(Md5Encryption.MD5("li0578"));
				user.setUserRoles(role_set);

				hsession = HibernateUtil.getSessionFactory()
						.openSession();
				transaction = null;

				System.out.println("Test add user only....");
				hsession = HibernateUtil.getSessionFactory().openSession();
				transaction = hsession.beginTransaction();

				System.out.println("User [" + hsession.save(user) + "] Saved.");
				transaction.commit();
				hsession.close();
			} catch (HibernateException e) {
				transaction.rollback();
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} finally {
				hsession.close();
			}
		}
	}
}
